import CryptoJS from 'crypto-js'
import { langConfig } from './lang.js'
import jsBridge from './jsBridge'
export const parseUrlParams = (url) => {
  url = location.href
  const index = url.indexOf('?') + 1
  const paramStr = url.substring(index)
  const arr = paramStr.split('&') //各个参数放到数组里
  const obj = {}
  for (var i = 0; i < arr.length; i++) {
    const num = arr[i].indexOf('=')
    if (num > 0) {
      const key = arr[i].substring(0, num)
      const value = arr[i].substr(num + 1)
      obj[key] = value
    }
  }
  if (obj.token) {
    obj['ignore-encryption'] = 'true'
    localStorage.setItem('appParams', JSON.stringify(obj))
  } else {
    jsBridge.goHomeCoins()
  }
  return obj
}

// 语言
export const getLang = () => {
  let appParams = localStorage.getItem('appParams')
  appParams = appParams
    ? JSON.parse(appParams)
    : {
        locale: 'en'
      }

  const { locale = 'en' } = appParams
  return langConfig[locale === 'es' ? 'es' : 'en']
  // console.log('appParams==', appParams)
  // return langConfig.es
}
export const getLangFn = () => {
  let appParams = localStorage.getItem('appParams')
  appParams = appParams
    ? JSON.parse(appParams)
    : {
        locale: 'en'
      }

  const { locale = 'en' } = appParams
  // return langConfig[locale] || langConfig.en
  return langConfig.ar || langConfig[locale]
}
export const getLangType = () => {
  let appParams = localStorage.getItem('appParams')
  appParams = appParams
    ? JSON.parse(appParams)
    : {
        locale: 'en'
      }

  return appParams
}
export const getUuid = () => {
  const tempUrl = URL.createObjectURL(new Blob())
  const uuid = tempUrl.toString() // blob:https://xxx.com/b250d159-e1b6-4a87-9002-885d90033be3
  URL.revokeObjectURL(tempUrl)
  return uuid.substr(uuid.lastIndexOf('/') + 1)
}

/**
 * 解密方法
 * @param data
 * @returns {string}
 */
const keysMap = {
  middleground: 'f02868d9179ae6dbbe42840d5ddbe1d8',
  bloomer: 'add79f5c0dc511eb822b54bf640796d6'
}
export const encrypt = (data, key) => {
  if (typeof data === 'object') {
    try {
      data = JSON.stringify(data)
    } catch (error) {
      console.log('encrypt error:', error)
    }
  }
  const SECRET_KEY = CryptoJS.enc.Hex.parse(keysMap[key])
  const encrypted = CryptoJS.AES.encrypt(data, SECRET_KEY, {
    // iv: SECRET_IV,
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  return encrypted.toString()
}

/**
 * 加密方法
 * @param text
 * @returns {string}
 */

export const decrypt = (text, key) => {
  const SECRET_KEY = CryptoJS.enc.Hex.parse(keysMap[key])
  const res = CryptoJS.AES.decrypt(text, SECRET_KEY, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  const decryptedStr = res.toString(CryptoJS.enc.Utf8)
  return decryptedStr
}
export const objectToQuery = (obj) => {
  let queryStr = ''
  Object.keys(obj).forEach((key, index) => {
    const t = index === 0 ? '?' : '&'
    queryStr += `${t}${key}=${obj[key]}`
  })
  return queryStr
}

// 1:需要清除 2:已清除
export const clearWebHistory = () => {
  const { href } = window.location
  window.history.pushState(null, null, href)
  return false
}
